package com.sgb.kjwl.viewmodel.self.carMakers;

import android.app.Application;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.alimaplibrary.util.ToastUtil;
import com.sgb.capital.view.ui.activity.OpenAccountFirstActivity;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.view.ui.activity.transport.PayQuotationOrderForCompanyActivity;
import com.swgk.core.base.api.ICallBack;
import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.OrderEntity;
import com.sgb.kjwl.model.entity.self.MyOfferEntity2;
import com.sgb.kjwl.model.entity.transport.AgreedCarOfferEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.util.UserOperating;
import com.sgb.kjwl.view.ui.activity.transport.PayQuotationOrderActivity;
import com.swgk.core.dialog.RoundDialog;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 杨畔
 * @date: 2021/2/6 16:42
 * @Description: 我的报价内容页viewModel
 */
public class MyOfferFragModel extends AndroidViewModel {
    public MutableLiveData<ListResponse<List<MyOfferEntity2>>> ldRecord = new MutableLiveData<>();
    private int page = 1;

    public MyOfferFragModel(Application application) {
        super(application);
    }

    public void getMyOfferOrder(Context context, int status, boolean isRefresh, Map<String, Object> map) {
        if (isRefresh) {
            page = 1;
        } else
            page++;
        DialogHelper.showProgressDialog(context, "数据加载中...");
        map.put("page", page);
        map.put("limit", 15);
        //竞价货源报价0、2接单 普通货源报价3
        map.put("searchType", 0);
        //货源模式 0 普通模式 1 竞价模式
        map.put("pattern", 1);
        if (status != 0)
            map.put("status", status);

        NewAPIManager.getInstance().getLogisticsAPI().getMyOfferList(map).enqueue(new ICallBack<BaseEntity<ListResponse<List<MyOfferEntity2>>>>() {
            @Override
            public void onSuccess(BaseEntity<ListResponse<List<MyOfferEntity2>>> data) {
                DialogHelper.dismissProgressDialog();
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    ldRecord.postValue(data.getData());
                    if (data.getData().haveMoreData()) {
                        page = data.getData().nextPage();
                    }
                } else {
                    ToastUtil.show(context, data.getMsg());
                }
            }

            @Override
            public void onFail() {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    /**
     * 检查车辆可使用状态
     */
    public void checkCarInfo(Context context, MyOfferEntity2 entity2) {
        Map map = new HashMap();
        map.put("carNo", entity2.getDriverNo());
        map.put("goodsSourceNo", entity2.getGoodsSourceNo());
        DialogHelper.showProgressDialog(context, "加载中...");
        NewAPIManager.getInstance().getLogisticsAPI().checkCarStatues(map).enqueue(new ICallBack<BaseEntity<AgreedCarOfferEntity>>() {
            @Override
            public void onSuccess(BaseEntity<AgreedCarOfferEntity> data) {
                DialogHelper.dismissProgressDialog();
                if (data == null)
                    return;
                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    if (data.getData() != null) {
                        AgreedCarOfferEntity offerEntity = data.getData();
                        if (TextUtils.isEmpty(offerEntity.getTerminal()))
                            return;

                        //检查货主
                        if (!offerEntity.isOpenFundsAccount() && offerEntity.getTerminal().equals("1")) {//货源货主企业没开通资金账户
                            new RoundDialog.Builder(context)
                                    .title("提示")
                                    .content("货主未开通收款账户，无法支付信息费，请联系货主开通资金账户")
                                    .confirm("确定")
                                    .singleBtn(true)
                                    .contentGravity(Gravity.CENTER)
                                    .cancelable(false)
                                    .show();
                        } else if (!offerEntity.isDriverOpenFundsAccount() && offerEntity.getTerminal().equals("2")) {//货源货主团队没开通资金账户
                            new RoundDialog.Builder(context)
                                    .title("提示")
                                    .content("货主未开通收款账户，无法支付信息费，请联系货主开通资金账户")
                                    .confirm("确定")
                                    .singleBtn(true)
                                    .contentGravity(Gravity.CENTER)
                                    .cancelable(false)
                                    .show();
                        } else if (!offerEntity.isDriverOpenFundsAccount() && offerEntity.getTerminal().equals("3")) {//个人货源资金账户
                            new RoundDialog.Builder(context)
                                    .title("提示")
                                    .content("货主未开通收款账户，无法支付信息费，请联系货主开通资金账户")
                                    .confirm("确定")
                                    .singleBtn(true)
                                    .contentGravity(Gravity.CENTER)
                                    .cancelable(false)
                                    .show();
                        }
                        //检查自己
                        else if (!UserOperating.getInstance().getUserInfo().isDriverOpenFundsAccount()) {
                            new RoundDialog.Builder(context)
                                    .title("提示")
                                    .content("开通在线收款账户可支付信息费")
                                    .cancel("取消")
                                    .confirm("前往开户")
                                    .contentGravity(Gravity.CENTER)
                                    .cancelable(false)
                                    .confirmCallback(v -> OpenAccountFirstActivity.start(context, true))
                                    .show();
                            return;
                        } else {
                            //检查车辆状态
                            if (!TextUtils.isEmpty(offerEntity.getVehicleStatus())) {
                                if (TextUtils.isEmpty(offerEntity.getCarNumber())) {
                                    new RoundDialog.Builder(context)
                                            .title("车辆状态异常")
                                            .content("未获取到车辆信息")
                                            .confirm("确定")
                                            .singleBtn(true)
                                            .contentGravity(Gravity.CENTER)
                                            .cancelable(false)
                                            .show();
                                    return;
                                }

                                if (offerEntity.getVehicleStatus().equals("1")) {
                                    //被下架
                                    new RoundDialog.Builder(context)
                                            .title("车辆状态异常")
                                            .content(offerEntity.getCarNumber() + "已被下架，请至“用户中心 - 车辆管理”中重新提交认证")
                                            .confirm("确定")
                                            .singleBtn(true)
                                            .contentGravity(Gravity.CENTER)
                                            .cancelable(false)
                                            .show();
                                } else if (offerEntity.getVehicleStatus().equals("2")) {
                                    //被删除
                                    new RoundDialog.Builder(context)
                                            .title("车辆状态异常")
                                            .content(offerEntity.getCarNumber() + "已被系统删除，无法使用。更多问题，请联系客服：400-001-0051")
                                            .confirm("确定")
                                            .singleBtn(true)
                                            .contentGravity(Gravity.CENTER)
                                            .cancelable(false)
                                            .show();
                                } else if (offerEntity.getVehicleStatus().equals("3")) {
                                    //所有状态正常——>前去支付
                                    OrderEntity orderEntity = new OrderEntity();
                                    orderEntity.setGoodsSourceNo(entity2.getGoodsSourceNo());
                                    orderEntity.setCarNumber(entity2.getCarNumber());
                                    orderEntity.setHeightHurdle(entity2.getHeightHurdle());
                                    orderEntity.setWeight(entity2.getWeight());
                                    orderEntity.setCarLen(entity2.getCarLenName());
                                    orderEntity.setCarType(entity2.getCarTypeName());
                                    orderEntity.setCityName(entity2.getCurrentCityName());
                                    orderEntity.setProvinceName(entity2.getCurrentProvinceName());
                                    orderEntity.setDistrictName(entity2.getCurrentDistrictName());
                                    orderEntity.setDetailAddress(entity2.getCurrentDetailAddress());
                                    orderEntity.setCarImage(entity2.getCarImage());
                                    orderEntity.setDriverNo(entity2.getDriverNo());
                                    orderEntity.setCarOfferNo(entity2.getCarOfferNo());
                                    orderEntity.setGoodsOfferNo(entity2.getGoodsOfferNo());
                                    orderEntity.setDiyField(entity2.getDiyField());
                                    orderEntity.setNote(entity2.getNote());
                                    orderEntity.setQuotation(true);
                                    orderEntity.setIsDump(entity2.getIsDump());
                                    orderEntity.setInTime(entity2.getInTime());
                                    if (!UserOperating.getInstance().isPersonal()) {
                                        PayQuotationOrderForCompanyActivity.start(context, entity2.getGoodsOfferNo());
                                    } else {
                                        PayQuotationOrderActivity.start(context, entity2.getGoodsOfferNo());
                                    }
                                }
                            }
                        }
                    }
                } else {
                    if (!TextUtils.isEmpty(data.getMsg()))
                        ToastUtil.show(context, data.getMsg());
                }
            }

        });
    }
}
